package com.dnake.fragment;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.dnake.Constant;
import com.dnake.entity.EventBusBean;
import com.dnake.entity.EventSettingBean;
import com.dnake.panel.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class PasswordOpenFragment extends DialogFragment {
    @Bind(R.id.tv_password)
    TextView tvPassword;
    private View mView;
    private ScanFaceTimer mScanFaceTimer;
    public static PasswordOpenFragment newInstance() {
        return new PasswordOpenFragment();
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE| DialogFragment.STYLE_NO_FRAME,0);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_password_open, container, false);
        ButterKnife.bind(this, mView);
        mScanFaceTimer = new ScanFaceTimer(20000, 1000);
        mScanFaceTimer.start();
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode() == Constant.EVENTBUS_PASSWORD_OPEN) {
            tvPassword.setText(eventBusBean.getName());
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
    /**
     * 超时关闭
     */
    class ScanFaceTimer extends CountDownTimer {

        public ScanFaceTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            dismiss();
        }
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        EventBus.getDefault().post(new EventBusBean(Constant.EVENTBUS_PASSWORD_CLOSE,""));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
